package com.lagou.edu.service.impl;

import com.lagou.edu.annotition.MyAutowired;
import com.lagou.edu.annotition.MyComponent;
import com.lagou.edu.annotition.MyTransactional;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.factory.BeanFactory;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;

import java.sql.Connection;

@MyComponent("transferService")
@MyTransactional
public class TransferServiceImpl implements TransferService {

//    private AccountDao accountDao = new JdbcAccountDaoImpl();

//	private AccountDao accountDao = (AccountDao) BeanFactory.getBean("accountDao");

	//最佳的状态
	@MyAutowired
	private AccountDao accountDao;

//	public AccountDao getAccountDao() {
//		return accountDao;
//	}
//
//	public void setAccountDao(AccountDao accountDao) {
//		this.accountDao = accountDao;
//	}

	@Override
	public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
		Account from = accountDao.queryAccountByCardNo(fromCardNo);
		Account to = accountDao.queryAccountByCardNo(toCardNo);

		from.setMoney(from.getMoney() - money);
		to.setMoney(to.getMoney() + money);

		accountDao.updateAccountByCardNo(from);
//		int c = 1 / 0;
		accountDao.updateAccountByCardNo(to);
	}
}
